package com.cloudptt.api.product.update;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.models.VersionInfo;
import com.cloudptt.api.product.log.Log;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.mcptt.comm.CommonFunc;
import com.mcptt.db.model.MapPointInfo;
import com.mcptt.http.operator.OkHttpUtils;
import com.mcptt.http.operator.online.OnlineParser;

import android.os.Handler;
import android.os.Message;
import android.util.Xml;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 *@author coolszy
 *@date 2012-4-26
 *@blog http://blog.92coding.com
 */
public class ParseVersionInfo implements Callback
{
	private CheckResultListener listener = null;
	public ParseVersionInfo()
	{
	}
	public int doCheckVersion(String url,CheckResultListener listener)
	{
		this.listener = listener;
//		Map<String, String> params = new HashMap<String, String>();
//		params.put("AppStyle", "" + type);

		OkHttpUtils.getInstance().doGet(url,this);//.doPutJson(url, params, this);
		return CauseCont.OperateCheck_Suc;
	}

	@Override
	public void onFailure(Call call, IOException e) {
		Log.debug(getClass(), "onFailure e = " + e);
		onFailure();
	}

	private void onFailure()
	{
		if(listener != null)
		{
			listener.onCheckResult(false,null);
		}
	}

	@Override
	public void onResponse(Call call, Response response) throws IOException {

		if(response == null)
		{
			onFailure();
		}

		Log.debug(getClass(), "onResponse isSuccessful = " + response.isSuccessful());
		if(response.isSuccessful())
		{

			InputStream  instreams = response.body().byteStream();
			String content = CommonFunc.convertStreamToString(instreams);//.convertStreamToString(instreams,"user_not_registered");
			Log.debug(getClass(), "onResponse Content = " + content);

			JsonParser jsonParser = new JsonParser();
			JsonObject obj = jsonParser.parse(content).getAsJsonObject();
			Log.debug(getClass(), "onResponse obj = " + obj);

			if(obj.get("versionCode") != null) {
				String str = obj.get("versionCode").getAsString();
				int version = CheckData.parseInt(str);
				if( version != -1
						&&  obj.get("versionName") != JsonNull.INSTANCE
						&& obj.get("must") != JsonNull.INSTANCE) {
					String name = obj.get("versionName").getAsString();
					VersionInfo info = new VersionInfo(name, version);
					String url = obj.get("url").getAsString();
					info.setUrl(url);
					str = obj.get("must").getAsString();
					info.setMust(str);
					if(obj.get("info") != JsonNull.INSTANCE) {
						str = obj.get("info").getAsString();
						info.addInfo(str);
					}

					if (listener != null) {
						listener.onCheckResult(true, info);
					}
					return;
				}
			}
		}
		onFailure();
	}
}
